/*
 Copyright (c) 2013- MeherTJ G.
 All rights reserved. Everybody can use these code freely.
 */

#pragma once

#include <sstream>
#include <string>
#include <vector>
#include <deque>
#include <cstdio>

namespace Zen {

	//	ToNumber(s to i)
	inline int64_t ToNumber(std::string const & s)
	{
#if 1
		return std::stoll(s);
#else
		int64_t res = 0;
		bool neg = false;
		size_t i = 0;
		while(isspace(s[i])) ++i;
		if(s[i] == '-') { neg = true; ++i; }
		else if(s[i] == '+') ++i;
		while(i < s.size())
		{
			if(!::isnumber(s[i])) break;
			res = res * 10 + (s[i] - '0');
		}
		if(neg) res = -res;
		return res;
#endif
	}
	
	inline std::string ToString(int64_t value)
	{
#if 1
		return std::to_string(value);
#else
		std::string res;
		bool neg = (value < 0);
		if(neg) value = -value;
		while(value)
		{
			auto n = value % 10;
			res.push_back('0' + n);
			value /= 10;
		}
		if(res.empty()) return "0";
		if(neg) res.push_back('-');
		std::reverse(res.begin(), res.end());
		return res;
#endif
	}

	/// std::to_string(double) remove
	inline std::string ToString(double v) {
		auto s = std::to_string(v);
		auto dot = s.find('.');
		if(dot == s.npos) return s;
		while(s.back() == '0') s.pop_back();
		if(s.back() == '.') s.push_back('0');
		return s;
	}

	//	ToString (data to s)
	template<typename _Array>
	inline std::string DataToString(_Array const & data)
	{
		return std::string(data.begin(), data.end());
	}
	
	inline std::string DataToString(void const * data, size_t size)
	{
		auto buf = (char const *)data;
		return std::string(buf, buf+size);
	}

	template<typename _Char>
	inline std::vector<_Char> StringToVector(std::string const & s)
	{
		static_assert(sizeof(_Char) == 1, "only char allowed here");
		return std::vector<_Char>(s.begin(), s.end());
	}
}

